﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Lottery.WPSOffice;

namespace Lottery.MediaData.Core {
    public static class Batch {
        /// <summary>
        /// 表一 数据合并
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static List<string> CombineT1(List<BatchDataItem> data,
            List<PeiLvDataItem> plData, int ccCount) {
            var result = new List<string>();
            for (int index = 0; index < data.Count; index++) {
                var item = data[index];
                var pl = plData[index];
                var plList = new List<int>();
                for (int i = 0; i < ccCount; i++) {
                    plList.Add(Convert.ToInt32(pl[i]));
                }

                for (int i = 0; i < 14; i++) {
                    if (plList.IndexOf(i + 1) == -1) {
                        continue;
                    }

                    var d = item[i].Three;
                    if (!string.IsNullOrEmpty(d) && result.IndexOf(d) == -1)
                        result.Add(d);

                    d = item[i].One;
                    if (!string.IsNullOrEmpty(d) && result.IndexOf(d) == -1)
                        result.Add(d);

                    d = item[i].Zero;
                    if (!string.IsNullOrEmpty(d) && result.IndexOf(d) == -1)
                        result.Add(d);
                }
            }

            return (from item in result orderby Convert.ToInt32(item) descending select item).ToList();
        }

        /// <summary>
        /// 生成表一的统计数据
        /// </summary>
        /// <param name="data">310数据</param>
        /// <param name="ycData">预测数据</param>
        /// <param name="plData">赔率数据</param>
        /// <param name="igData">不参与合并的数据</param>
        /// <param name="c">合并行</param>
        /// <param name="ccCount">场次</param>
        /// <param name="fileName">保存的Excel文件名</param>
        public static List<List<string>> GenT1(List<BatchDataItem> data, List<YuCeDataItem> ycData,
            List<PeiLvDataItem> plData, List<string> igData, CL c, int ccCount) {
            if (ccCount > 14 || ccCount < 3) {
                throw new ApplicationException("场次数不在3到14之间");
            }

            var listAllMediaCount = CombineT1(data, plData, ccCount);
            var result = new List<List<string>>();

            for (int index = 0; index < data.Count; index++) {
                var item = data[index];
                var pl = plData[index];
                var plList = new List<int>();
                for (int i = 0; i < ccCount; i++) {
                    plList.Add(Convert.ToInt32(pl[i]));
                }

                var allDatas = new List<DS>();
                for (int i = 0; i < 14; i++) {
                    var it = item[i];
                    if (plList.IndexOf(i + 1) != -1)
                        allDatas.Add(new DS(Convert.ToInt32(it.Three),
                            Convert.ToInt32(it.One), Convert.ToInt32(it.Zero), string.Empty));
                    else
                        allDatas.Add(new DS(int.MinValue, int.MinValue, int.MinValue, string.Empty));
                }
                var r = DSHelper.ProcessAllData(allDatas);
                var com = new List<FD>();
                foreach (var ri in r.Keys) {
                    if (ri == int.MinValue) continue;

                    var isAttended = igData.IndexOf(ri.ToString()) == -1;
                    com.Add(new FD(ri, r[ri], 0, r[ri], isAttended));
                }
                var ft = new FTCellData();
                MDHelper.Gen(ft, allDatas, com, "t1");

                CL cl = new CL();
                var list = from listItem in r.Keys
                           where listItem >= c.From1 && listItem <= c.To1
                           select listItem;

                if (list.Count() < 1) {
                    //var info = string.Format("预测次数号为{0}的数据在区域一中没有媒体数据", item.Order);
                    //throw new ApplicationException(info);
                    cl.From1 = int.MinValue;
                    cl.To1 = int.MinValue;
                } else {
                    cl.From1 = list.Min();
                    cl.To1 = list.Max();
                }

                list = from listItem in r.Keys
                       where listItem >= c.From2 && listItem <= c.To2
                       select listItem;

                if (list.Count() < 1) {
                    //var info = string.Format("预测次数号为{0}的数据在区域二中没有媒体数据", item.Order);
                    //throw new ApplicationException(info);
                    cl.From2 = int.MinValue;
                    cl.To2 = int.MinValue;
                } else {
                    cl.From2 = list.Min();
                    cl.To2 = list.Max();
                }

                MDHelper.CombineLine(ft, com, cl);

                // 模拟开奖
                var numbers = new List<string>();
                var yc = ycData[index];
                for (int i = 0; i < 14; i++) {
                    numbers.Add(yc[i]);
                }

                DataHelper.DoSimulation(numbers.ToArray(), ft);

                var itemResult = new List<string>();
                foreach (var itemMD in listAllMediaCount) {
                    if (r.Keys.Contains(Convert.ToInt32(itemMD))) {
                        for (int i = 0; i < com.Count; i++) {
                            var fd = com[i];
                            if (fd.MediaCount == Convert.ToInt32(itemMD)) {
                                itemResult.Add(fd.Count.ToString());
                                itemResult.Add(ft[16, i]);

                                break;
                            }
                        }
                    } else {
                        itemResult.Add(string.Empty);
                        itemResult.Add(string.Empty);
                    }
                }
                for (int i = com.Count; i < ft.RowCount; i++) {
                    itemResult.Add(ft.GetRowValidCount(i).ToString());
                    itemResult.Add(ft[16, i]);
                }

                result.Add(itemResult);
            }

            return result;
        }

        public static void SaveExcelT1(string fileName, List<List<string>> data,
            List<string> xhList, List<string> md) {
            BatchGen.ExportT1(fileName, data, xhList, md);
        }

        /// <summary>
        /// 表二 数据合并
        /// </summary>
        /// <param name="m_data"></param>
        /// <param name="m_plData">赔率数据</param>
        /// <param name="ccCount">场次数</param>
        /// <returns></returns>
        public static List<string> CombineT2(List<BatchDataItemT2> data, List<PeiLvDataItem> plData, int ccCount) {
            var result = new List<string>();
            for (int index = 0; index < data.Count; index++) {
                var item = data[index];
                var pl = plData[index];
                var plList = new List<int>();
                for (int i = 0; i < ccCount; i++) {
                    plList.Add(Convert.ToInt32(pl[i]));
                }

                for (int i = 0; i < 14; i++) {
                    if (plList.IndexOf(i + 1) == -1) {
                        continue;
                    }

                    var d = item[i].D;
                    if (!string.IsNullOrEmpty(d) && result.IndexOf(d) == -1)
                        result.Add(d);

                    d = item[i].Z;
                    if (!string.IsNullOrEmpty(d) && result.IndexOf(d) == -1)
                        result.Add(d);

                    d = item[i].X;
                    if (!string.IsNullOrEmpty(d) && result.IndexOf(d) == -1)
                        result.Add(d);
                }
            }

            return (from item in result orderby Convert.ToInt32(item) descending select item).ToList();
        }

        public static List<List<string>> GenT2(List<BatchDataItemT2> data, List<YuCeDataItem> ycData,
            List<PeiLvDataItem> plData, List<string> igData, CL c, int ccCount) {
            if (ccCount > 14 || ccCount < 3) {
                throw new ApplicationException("场次数不在3到14之间");
            }

            var allMediaData = CombineT2(data, plData, ccCount);

            var result = new List<List<string>>();

            for (int index = 0; index < data.Count; index++) {
                var item = data[index];
                var pl = plData[index];
                var plList = new List<int>();
                for (int i = 0; i < ccCount; i++) {
                    plList.Add(Convert.ToInt32(pl[i]));
                }

                var allData = new List<SC>();
                for (int i = 0; i < 14; i++) {
                    var it = item[i];
                    if (plList.IndexOf(i + 1) != -1) {
                        allData.Add(new SC() {
                            D = it.D, Z = it.Z,
                            X = it.X, Three = it.Three, One = it.One,
                            Zero = it.Zero, PreNumber = it.PreNumber
                        });
                    } else {
                        allData.Add(new SC() {
                            D = "", Z = "", X = "", PreNumber = "",
                            Three = int.MinValue, One = int.MinValue, Zero = int.MinValue
                        });
                    }
                }
                var r = SCHelper.ProcessAllData(allData);
                var com = new List<FD>();
                foreach (var ri in r.Keys) {
                    var isAttended = igData.IndexOf(ri.ToString()) == -1;
                    com.Add(new FD(ri, r[ri], 0, r[ri], isAttended));
                }
                var ft = new FTCellData();
                SCHelper.Gen(ft, allData, com, "t2");

                CL cl = new CL();
                var list = from listItem in r.Keys
                           where listItem >= c.From1 && listItem <= c.To1
                           select listItem;

                if (list.Count() < 1) {
                    //var info = string.Format("预测次数号为{0}的数据在区域一中没有媒体数据", item.Order);
                    //throw new ApplicationException(info);
                    cl.From1 = int.MinValue;
                    cl.To1 = int.MinValue;
                } else {
                    cl.From1 = list.Min();
                    cl.To1 = list.Max();
                }

                list = from listItem in r.Keys
                       where listItem >= c.From2 && listItem <= c.To2
                       select listItem;

                if (list.Count() < 1) {
                    //var info = string.Format("预测次数号为{0}的数据在区域二中没有媒体数据", item.Order);
                    //throw new ApplicationException(info);
                    cl.From2 = cl.To2 = int.MinValue;
                } else {
                    cl.From2 = list.Min();
                    cl.To2 = list.Max();
                }

                SCHelper.CombineLine(ft, com, cl);

                // 模拟开奖
                var numbers = new List<string>();
                var yc = ycData[index];
                for (int i = 0; i < 14; i++) {
                    numbers.Add(yc[i]);
                }

                DataHelper.DoSimulation(numbers.ToArray(), ft);

                var itemResult = new List<string>();
                foreach (var itemMD in allMediaData) {
                    if (r.Keys.Contains(Convert.ToInt32(itemMD))) {
                        for (int i = 0; i < com.Count; i++) {
                            var fd = com[i];
                            if (fd.MediaCount == Convert.ToInt32(itemMD)) {
                                itemResult.Add(fd.Count.ToString());
                                itemResult.Add(ft[16, i]);

                                break;
                            }
                        }
                    } else {
                        itemResult.Add(string.Empty);
                        itemResult.Add(string.Empty);
                    }
                }
                for (int i = com.Count; i < ft.RowCount; i++) {
                    itemResult.Add(ft.GetRowValidCount(i).ToString());
                    itemResult.Add(ft[16, i]);
                }

                result.Add(itemResult);
            }

            return result;
        }

        public static void SaveExcelT2(string fileName, List<List<string>> data, List<string> xhList, List<string> md) {
            SaveExcelT1(fileName, data, xhList, md);
        }
    }
}